/**
 * Created by admin on 2018/4/14.
 */


$(document).ready(
    function() {
        $("form").submit(function() {
            return false;
        });
        var VAL = {
            NORMAL : "0000000",
            EVT_USER : $("#userId").text(),
            T_MWPHALFWHIN: "MWPHALFWHIN",
            T_MIPWOBOM: "MIPWOBOM",
            T_FIPINQSHT: "FIPINQSHT",
            T_FDPAPLYWO: "FDPAPLYWO",
            T_FBPLSTDAT : "FBPBISDAT",
            DISABLED_ATTR : {
                "disabled" : true
            },
            ENABLED_ATTR : {
                "disabled" : false
            }
        };
        var domObj = {
            $window : $(window),

            $prdSeqId   : $("#prdSeqId"),
            $mtrlProcId : $("#mtrlProcId"),
            $destId   : $("#destId"),
            $woIdSel   : $("#woIdSel"),
            grids : {
                $prdInfoGrd : $("#prdInfoGrd"),
            },
            buttons : {
                $query_btn: $("#query_btn"),
                $register_btn: $("#register_btn"),
                $clean_btn: $("#clean_btn")
            }

        };

        function initPrdInfoGrd() {
            domObj.grids.$prdInfoGrd.jqGrid({
                url : "",
                datatype : "local",
                mtype : "POST",
                height : 400,
                width : $("#prdInfoGrdDiv").width() * 0.99,
                autowidth: false,
                shrinkToFit:false,
                autoScroll: false,
                resizable : true,
                loadonce : true,
                viewrecords : true,
                rownumbers : true,
                rownumWidth : 30,
                rowNum : 20,
                emptyrecords : true,
                pager : "prdInfoPg",
                multiselect : true,
                colModel : [ {
                    name : 'prd_seq_id',
                    index : 'prd_seq_id',
                    label : '屏体代码',
                    width : 180
                }, {
                    name : 'wo_id_fk',
                    index : 'wo_id_fk',
                    label : '工单',
                    width : 180
                }, {
                    name : 'mdl_id_fk',
                    index : 'mdl_id_fk',
                    label : '产品名称',
                    width : 180
                }, {
                    name : 'prd_stat',
                    index : 'prd_stat',
                    label : '屏体状态',
                    width : 180
                }, {
                    name : 'whin_stat',
                    index : 'whin_stat',
                    label : '入库状态',
                    width : 180
                }, {
                    name  : 'cr_ope_id_fk',
                    index : 'cr_ope_id_fk',
                    label : '当前站点',
                    width : 180
                },{
                    name : 'dest_shop',
                    index : 'dest_shop',
                    label : '仓位信息',
                    width : 180
                }]
            });
        }
        var toolFnc = {
            initWoIdFnc: function () {
                var inObj, outObj;
                inObj = {
                    trx_id: VAL.T_FDPAPLYWO,
                    action_flg: 'Q',
                    iary: []
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    var oaryA = outObj.oary;
                    var compare = function (obj1, obj2) {
                        var val1 = obj1.wo_id;
                        var val2 = obj2.wo_id;
                        if (val1 < val2) {
                            return -1;
                        } else if (val1 > val2) {
                            return 1;
                        } else {
                            return 0;
                        }
                    }
                    oaryA.sort(compare);
                    SelectDom.addSelectArr(domObj.$woIdSel,oaryA, "wo_id", "wo_id", "", true);
                }
            },
            initMtrlProc : function (woIdFk) {
                var inObj = {
                    trx_id : VAL.T_MIPWOBOM,
                    wo_id_fk :woIdFk,
                    action_flg : "R",
                    mtrl_type :"X"
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    SelectDom.addSelectArr(domObj.$mtrlProcId, outObj.oaryA, "mtrl_prod_id_fk","mtrl_prod_id_fk","",true);
                }
            },
            initDestShop : function () {
                var inObj = {
                    trx_id : VAL.T_FBPLSTDAT,
                    action_flg : "Q",
                    iary : [{
                        data_cate: 'DEST',
                        data_item  : 'L'
                    }]
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    SelectDom.addSelectArr(domObj.$destId, outObj.oary, "data_ext","data_desc","",true);
                }
            },
        }
        var buttonFnc = {
            queryFnc : function (){
                var prdSeqId = $.trim(domObj.$prdSeqId.val());
                var woIdFk   = $.trim(domObj.$woIdSel.val());
                if(!(prdSeqId || woIdFk)){
                    showErrorDialog("", "请输入屏体代码或者工单号");
                    return false;
                }
                var inObj = {
                    trx_id : VAL.T_MWPHALFWHIN,
                    action_flg : "Q",
                    prd_seq_id : prdSeqId,
                    wo_id_fk   : woIdFk,
                    iary : []
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if(outObj.rtn_code === VAL.NORMAL){
                    setGridInfo(outObj.oary, domObj.grids.$prdInfoGrd, true);
                }
            },
            registerFnc : function (){
                var destId = $.trim(domObj.$destId.val());
                if(!destId){
                    showErrorDialog("", "仓位不能为空");
                    return false;
                }
                var mtrlProcId = $.trim(domObj.$mtrlProcId.val());
                if(!mtrlProcId){
                    showErrorDialog("", "半成品料号不能为空");
                    return false;
                }
                ids = domObj.grids.$prdInfoGrd.jqGrid('getGridParam', 'selarrrow');
                if(ids.length<=0){
                    showErrorDialog("", "请选择要入库的屏体");
                    return false;
                }
                var iary=[];
                for(var i=0;i<ids.length;i++){
                    rowData = domObj.grids.$prdInfoGrd.jqGrid("getRowData", ids[i]);
                    iary.push(rowData);
                }
                var inObj = {
                    trx_id : VAL.T_MWPHALFWHIN,
                    action_flg : "I",
                    evt_usr: VAL.EVT_USER,
                    dest_shop : destId,
                    mtrl_proc_id : mtrlProcId,
                    iary : iary
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if(outObj.rtn_code === VAL.NORMAL){
                    var length= ids.length;
                   for(var i=0;i<length;i++){
                       domObj.grids.$prdInfoGrd.jqGrid("delRowData",ids[0]);
                   }
                    showSuccessDialog("半成品入库成功");
                }
            },
            cleanFnc : function() {
                domObj.$prdSeqId.val("");
                SelectDom.initWithSpace(domObj.$mtrlProcId);
                SelectDom.initWithSpace(domObj.$destId);
                SelectDom.initWithSpace(domObj.$woIdSel);
                domObj.grids.$prdInfoGrd.jqGrid('clearGridData');
                toolFnc.initDestShop();
                toolFnc.initWoIdFnc();
            },
        };
        var iniButtonAction = function() {
            domObj.buttons.$query_btn.bind('click', buttonFnc.queryFnc);
            domObj.buttons.$register_btn.bind('click', buttonFnc.registerFnc);
            domObj.buttons.$clean_btn.bind('click', buttonFnc.cleanFnc);
        }
        domObj.$prdSeqId.keydown(function(event) {
            var inObj, outObj,  prdSeqId;
            if (event.keyCode == 13) {
                prdSeqId = domObj.$prdSeqId.val();
                inObj = {
                    trx_id : VAL.T_MWPHALFWHIN,
                    action_flg : 'Q',
                    prd_seq_id : prdSeqId
                },
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code === VAL.NORMAL) {
                    if(outObj.oary.length>0){
                       var outObj = outObj.oary[0];
                        if( outObj.prd_stat != "WAIT"){
                            showErrorDialog("", "当前屏体状态只能是WAIT");
                            return false;
                        }
                        var rowDatas = $("#prdInfoGrd").jqGrid("getRowData");
                        if(rowDatas.length>0){
                            for (var i = 0; i < rowDatas.length; i++) {
                                if ($.trim(rowDatas[i].prd_seq_id) ==$.trim(prdSeqId) ) {
                                    domObj.$prdSeqId.val("");
                                    return;
                                }
                                if ($.trim(rowDatas[i].wo_id_fk) != outObj.wo_id_fk) {
                                    showErrorDialog("", "该屏体工单["+rowDatas[i].wo_id_fk+"]与列表中工单不一致！");
                                    return;
                                }
                                if ($.trim(rowDatas[i].cr_ope_id_fk) != outObj.cr_ope_id_fk) {
                                    showErrorDialog("", "该屏体当前站点["+rowDatas[i].cr_ope_id_fk+"]与列表中工单当前站点不一致！");
                                    return;
                                }
                            }
                        }
                        var newRowID = getGridNewRowID("#prdInfoGrd");
                        $("#prdInfoGrd").jqGrid("addRowData",newRowID,outObj);
                        //初始化半成品料号
                        toolFnc.initMtrlProc(outObj.wo_id_fk);
                        domObj.$prdSeqId.val("");
                        domObj.$prdSeqId.focus();
                    }
                }
            }
        });
        function initPage(){
            domObj.$prdSeqId.val("");
            toolFnc.initWoIdFnc();
            toolFnc.initDestShop();
            domObj.grids.$prdInfoGrd.jqGrid('clearGridData');
            initPrdInfoGrd();
            iniButtonAction();
        };
        initPage();

        //表格自适应
        function resizeFnc(){
            var offsetBottom, divWidth;
            divWidth = $("#prdInfoGrdDiv").width();
            offsetBottom =  $(window).height() - $("#prdInfoGrdDiv").offset().top;
            $("#prdInfoGrdDiv").height(offsetBottom * 0.95);
            $("#prdInfoGrd").setGridWidth(divWidth * 0.99);
            $("#prdInfoGrd").setGridHeight(offsetBottom * 0.99 - 51);

        };
        resizeFnc();
        $(window).resize(function() {
            resizeFnc();
        });
    })